package com.lu.controller;

import com.lu.bean.*;
import com.lu.constant.ErrorEnum;
import com.lu.service.GameService;
import com.lu.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 游戏接口
 * @author issuser
 * @ClassName GameContoller
 * @date 2021/07/12 19:36
 */
@Slf4j
@RestController
@Api(tags = "游戏接口")
public class GameController {

    @Resource
    private GameService gameService;

    /**
     * 管理员开始游戏
     * @author luke
     * @date 2021/7/13 9:57
     * @param openIds
     * @return com.lu.bean.ResBean
     */
    @RequestMapping(value = "/game/createNewGame", method = RequestMethod.POST)
    @ApiOperation("创建游戏")
    public ResBean createNewGame(@ApiParam(value = "参与游戏的人员openid,使用','隔开，最少2人", required = true) @RequestParam String openIds) {
        ResBean res = new ResBean();
        try {
            String gameId = gameService.createNewGame(openIds);
            res.setData(gameId);
        } catch (AppException e) {
            log.error("AppException occur,{},", e, e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }

    /**
     * 用户选词
     * @author luke
     * @date 2021/7/13 11:39
     * @param gameId
     * @param openId
     * @param riddle
     * @return com.lu.bean.ResBean
     */
    @RequestMapping(value = "/game/createBranch", method = RequestMethod.POST)
    @ApiOperation("用户选词")
    public ResBean createBranch(@ApiParam(value = "创建游戏时生成的游戏id，", required = true) @RequestParam String gameId,
                                @ApiParam(value = "玩家openid", required = true) @RequestParam String openId,
                                @ApiParam(value = "玩家所选的词语（谜底）", required = true) @RequestParam String riddle) {
        ResBean res = new ResBean();
        try {
            String branchId = gameService.createBranch(gameId, openId, riddle);
            res.setData(branchId);
        } catch (AppException e) {
            log.error("AppException occur,{},",e.toString(),e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }

    /**
     * 用户查看流转到自己的任务
     * @author luke
     * @date 2021/7/13 15:24
     * @param gameId
     * @param openId
     * @return com.lu.bean.ResBean
     */
    @RequestMapping(value = "/game/queryTask", method = RequestMethod.POST)
    @ApiOperation("用户查看流转到自己的任务")
    public ResBean queryTask(@ApiParam(value = "创建游戏时生成的游戏id，", required = true) @RequestParam String gameId,
                             @ApiParam(value = "玩家openid", required = true) @RequestParam String openId) {
        ResBean res = new ResBean();
        try {
            List<TaskBean> list = gameService.queryTask(gameId, openId);
            res.setData(list);
        } catch (AppException e) {
            log.error("AppException occur,{},",e.toString(),e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }

    /**
     * 获取玩家参与的子游戏列表
     * @author luke
     * @date 2021/7/14 14:04
     * @param openId
     * @return com.lu.bean.ResBean
     */
    @RequestMapping(value = "/game/history/list", method = RequestMethod.POST)
    @ApiOperation("获取玩家参与的子游戏列表")
    public ResBean queryHistoryList(@ApiParam(value = "玩家openid", required = true) @RequestParam String openId) {
        ResBean res = new ResBean();
        try {
            ParamUtil.checkStringEmpty(openId,"openId");
            List<BranchBean> list = gameService.queryHistoryList(openId);
            res.setData(list);
        } catch (AppException e) {
            log.error("AppException occur,{},",e.toString(),e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }

    /**
     * 获取子游戏的提交记录，其中orderNum -1 为谜底，首个玩家提交orderNum从 0 开始
     * @author luke
     * @date 2021/7/14 14:05
     * @param branchId
     * @param orderNum
     * @return com.lu.bean.ResBean
     */
    @RequestMapping(value = "/game/history/detail", method = RequestMethod.POST)
    @ApiOperation("获取子游戏的提交记录")
    public ResBean queryHistoryDetail(@ApiParam(value = "游戏支线id", required = true) @RequestParam String branchId,
                                      @ApiParam(value = "提交序号，-1为谜底", required = true) @RequestParam String orderNum) {
        ResBean res = new ResBean();
        try {
            ParamUtil.checkStringEmpty(branchId,"branchId");
            ParamUtil.checkStringEmpty(orderNum,"orderNum");
            List<SubmitBean> list = gameService.queryHistoryDetail(branchId, orderNum);
            res.setData(list);
        } catch (AppException e) {
            log.error("AppException occur,{},",e.toString(),e);
            res.setByAppException(e);
        } catch (Exception e) {
            log.error("Exception occur,",e);
            res.setResCode(ErrorEnum.FAIL.code);
            res.setResMsg("服务器异常");
        }
        return res;
    }


}
